﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	import flash.text.TextField;

	public class PlayerShapeObject extends BasicShapeObject {
		
		/* The ID of a ShapeObject is based on two numbers
		with the next format: id.stage; where id is an increasing
		number and the stage is the ID of the stage.
		Please note any of both parts is assured to be of secuential
		increasing value.
		*/
		private var id:			String; // id.stage
		private var text_txt:	TextField;
		private var iniWidth:	Number;
		private var iniHeight:	Number;
		
		// ------  Public variables ------
		
		// These variables are public because their only purpose is to
		// be accesible to other classes, they do not affect this class
		// at all... so why have them? Well, Tween class in Actionscript3
		// needs something to modify in order to trigger all needed events
		// by the player, so each of these will be modified by their
		// corresponding Tween
		public var textChange:	Number;
		public var depthChange:	Number;
		public var paintChange: Number;
		
		// Blur of the shape, are public because of what was explained
		// before, but these do affect the class
		public var blurX:	Number;
		public var blurY:	Number;
		/******** STATIC FUNCTIONS ********/
		
		/**
		Returns the first part of the str, excluding the point
		@param str String with full ID of the Shape
		*/
		public static function IDof(str:String):String{
			return str.substring(0, str.indexOf("."));
		}
		
		/**
		Returns the second part of the str, excluding the point
		@param str String with full ID of the Shape
		*/
		public static function SceneOf(str:String):String{
			return str.substring(str.indexOf(".")+1, str.length);
		}
		
		/******** PUBLIC FUNCTIONS ********/
		
		// private var text_txt:TextField;
		public function PlayerShapeObject(){
			alpha 		= 0;
			id 			= "0.0";
			iniWidth 	= width;
			iniHeight 	= height;
			blurX		= 0;
			blurY		= 0;
			if(BasicShapeObjectDatabase.hasText(this)){
				text_txt = BasicShapeObjectDatabase.getTextField(this);
				this.addChild(text_txt);
			}
		}
		
		/**
		Sets the ID of the ShapeObject
		@param num ID of the object
		*/
		public function setID(num:uint):void{
			id = String(num) + "." + SceneOf(id);
		}
		
		/**
		Gets the ID of the ShapeObject inside the scene
		@ret ID of the object in uint form
		*/
		public function getID():uint{
			return uint(IDof(id));
		}
		
		public function setStageID(num:uint):void{
			id = IDof(id) + "." + String(num);
		}
		
		public function getStageID():uint{
			return uint(SceneOf(id));
		}
		
		public function setFullID(fid:String):void{
			id = fid;
		}
		
		public function getFullID():String{
			return id;
		}
		
		override public function getWidth():Number{
			return iniWidth;
		}
		
		override public function getHeight():Number{
			return iniHeight;
		}
		
		public function setText(txt:String):void{
			text_txt.text = txt;
		}
		
		/**
		  Repaints this shape with the given lines and colors
		*/
		public function paint(array:Array):void{
			var colorArray:Array = array[0]; // Array[Array[color:uint, numcoloredlines:uint]]
			var linesArray:Array = array[1]; // Array[Array[Array[x, y]]]
			var i:uint = 0;
			var j:uint = 0;
			var color:uint = 0;
			var nextColor:uint = 0;
			var point:Array;
			var first:Boolean = true;
			
			
			this.graphics.clear();
			for(i=0; i<linesArray.length; ++i){ // For each line
				if(i == nextColor){
					nextColor = i+colorArray[color][1];
					graphics.lineStyle(5, colorArray[color][0]);
					++color;
				}
				first = true;
				for(j=0; j<linesArray[i].length; ++j){
					point = linesArray[i][j];
					if(first){
						first = false;
						graphics.moveTo(point[0], point[1]);
					}else{
						graphics.lineTo(point[0], point[1]);
					}
				}
			}
		}
		
	}
}